package com.ikas.ai.server.module.steady.service;

import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ikas.ai.server.module.steady.dao.SteadyStateMeteMapper;
import com.ikas.ai.server.module.steady.enums.SteadyExecStatusEnum;
import com.ikas.ai.server.module.steady.model.dto.StyStateMeteQueryDto;
import com.ikas.ai.server.module.steady.model.po.SteadyStateMete;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 稳态测点表 服务实现类
 * </p>
 *
 * @author wuyf
 * @since 2023-12-13
 */
@Slf4j
@Service
public class SteadyStateMeteService extends ServiceImpl<SteadyStateMeteMapper, SteadyStateMete> {


    @Autowired
    private SteadyStateMeteMapper steadyStateMeteMapper;

    @Value("${steady.mete.time:86400000}")
    private Long seconds;

    private static final String REMARK = "稳态测点定时任务更新状态%s";


    public List<SteadyStateMete> listStyStateMete(StyStateMeteQueryDto queryDto) {
        LambdaQueryWrapper<SteadyStateMete> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectUtils.isNotEmpty(queryDto.getSteadyType()), SteadyStateMete::getSteadyType, queryDto.getSteadyType());
        queryWrapper.eq(ObjectUtils.isNotEmpty(queryDto.getSteadyStatus()), SteadyStateMete::getSteadyStatus, queryDto.getSteadyStatus());
        queryWrapper.like(StringUtils.isNotBlank(queryDto.getMeteName()), SteadyStateMete::getMeteName, queryDto.getMeteName());
        queryWrapper.eq(StringUtils.isNotBlank(queryDto.getMeteCode()), SteadyStateMete::getMeteCode, queryDto.getMeteCode());
        queryWrapper.eq(StringUtils.isNotBlank(queryDto.getMachineNo()), SteadyStateMete::getMachineNo, queryDto.getMachineNo());
        return this.list(queryWrapper);
    }

    public void updateSteadyStateMete() {
        //查找出所有已完成状态的停机稳态测点，如果测点的更新时间超市未更新，将s设为处理中状态
        LambdaQueryChainWrapper<SteadyStateMete> wrapper = new LambdaQueryChainWrapper<>(steadyStateMeteMapper);
        List<SteadyStateMete> steadyStateMeteList = wrapper.in(SteadyStateMete::getSteadyStatus,SteadyExecStatusEnum.NOT_START.getValue(),SteadyExecStatusEnum.PROCESSING.getValue()).list();
//        steadyStateMeteList = steadyStateMeteList.stream().filter(s -> SteadyExecStatusEnum.COMPLETE.getValue().equals(s.getSteadyStatus())).collect(Collectors.toList());
        List<SteadyStateMete> updateList = new ArrayList<>();
        steadyStateMeteList.forEach(s -> {
            Date updateTime = s.getUpdateTime();
            DateTime start = DateTime.of(DateTime.now().getTime() - seconds);
            if (DateTime.of(updateTime).isBefore(start)) {
                //如果超时更新状态为2
                s.setSteadyStatus(SteadyExecStatusEnum.COMPLETE.getValue());
                s.setUpdateTime(new Date());
                s.setStartTime(s.getStartTime());
                s.setEndTime(DateTime.now().toTimestamp());
                s.setRemark(String.format(REMARK, DateTime.now()));
                updateList.add(s);
            }
        });
        log.info("更新停机稳态测点:{}", JSON.toJSONString(updateList));
        updateBatchById(updateList);
    }
}